<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="handleClosed"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="formRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="算子名称" prop="clientName">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="算子分类" prop="clientName">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="算子描述" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            v-model="form.remark"
            :maxlength="200"
            show-word-limit
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions  } from 'vuex'

export default {
  name: 'addOrEdit',
  props: ['mode', 'data'],
  data () {
    return {
      options: [
        {
          label: '数值型',
          value: 'basic'
        },
        {
          label: '算法',
          value: 'math'
        },
        {
          label: '...',
          value: 'string'
        }
      ],
      dialogVisible: true,
      title: this.mode === 1 ? '编辑' : '添加',
      form: {
        clientName: '',
        remark: ''
      },
      rules: {
        clientName: { required: true, message: '请输入' }
      }
    }
  },
  created () {
    if (+this.mode) {
      const { clientName, remark } = this.data
      this.form = { clientName, remark }
    }
  },
  methods: {
    ...mapActions({
      setAccessToken: 'SET_ACCESSTOKEN',
      updateAccessToken: 'UPDATE_ACCESSTOKEN'
    }),
    handleConfirm () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.editAccessToken()
          // const params = {
          //   ...this.form,
          //   projectId: this.$route.params.projectId,
          // }
          // if (this.mode) {
          //   // 编辑
          //   this.updateAccessToken(params).then((res) => {
          //     const { code } = res
          //     if (code === 200) {
          //       this.$message.success('操作成功')
          //       this.$emit('success', this.mode)
          //       this.dialogVisible = false
          //     } else {
          //       this.$message.error('操作失败')
          //     }
          //   })
          // } else {
          //   // 新增
          //   this.setAccessToken(params).then((res) => {
          //     const { code } = res
          //     if (code === 200) {
          //       this.$message.success('操作成功')
          //       this.$emit('success', this.mode)
          //       this.dialogVisible = false
          //     } else {
          //       this.$message.error('操作失败')
          //     }
          //   })
          // }
        }
      })
    },
    async editAccessToken () {
      try {
        this.isSubmiting = true
        const params = this.getParams()
        let res = null
        if (this.mode) {
          res = await this.updateAccessToken({ id: this.data.id, params }) // 编辑
        } else {
          res = await this.setAccessToken(params) // 新增
        }
        if (res.code === 200) {
          this.$message.success('操作成功')
          this.$emit('success', this.mode)
          this.dialogVisible = false
        } else {
          this.$message.error('操作失败')
        }
      } catch (e) {
        console.log(e)
      } finally {
        this.isSubmiting = false
      }
    },
    getParams () {
      return {
        ...this.form,
        projectId: this.$route.params.projectId
      }
    },
    handleClosed () {
      this.$emit('closed')
    }
  }
}
</script>

<style lang="scss" scoped></style>
